package de.jonasjelten.jcalc.mathExps;

import de.jonasjelten.jcalc.api.JCalc;
import de.jonasjelten.jcalc.exception.JEvaluationException;
import de.jonasjelten.jcalc.math.JNumber;
import de.jonasjelten.jcalc.math.JDualNumber;

/**
 *
 * @author michael
 */
public abstract class JDualArgMathExp extends JMathExp {
    JMathExp arg0, arg1;

    public JDualArgMathExp(JMathExp arg0, JMathExp arg1) {
        super(arg0.jcalc);
        this.arg0 = arg0;
        this.arg1 = arg1;
    }

    public JDualNumber evalChildren() {
        arg0.init();
        arg1.init();

        // the getEvalResult() functions will automatically wait for the
        // subthreads to finish.
        return new JDualNumber(arg0.getEvalResult(), arg1.getEvalResult());
    }

    @Override
    public abstract JNumber eval() throws JEvaluationException;

    public String toString(boolean bracket, String operand) {
        if (bracket) {
            return "(" + arg0 + operand + arg1 + ")";
        } else {
            return arg0 + operand + arg1;
        }
    }

    @Override
    public abstract boolean isTrivial();

    @Override
    public int getNonTrivialChildCount() {
        return arg0.getNonTrivialMathExpsCount() + arg1.getNonTrivialMathExpsCount();
    }
}
